<?php

class GalactusModelPost extends GalactusModelCalendar {

	function __construct($control) {
		GalactusModel::__construct($control);
	}

	public function add($title, $content) {
		$query = $this->query(
			"INSERT INTO Post (user_id, created_on, title, content) VALUES (:user_id, NOW( ), :title, :content)",
			array(":user_id" => $this->control->user_id, ":title" => $title, ":content" => $content)
		);
		if(!$query["fail"])
			echo $this->control->view->helper->alertSuccess("Recado inserido com sucesso!", URL."REDIRECT/POST/SHOW");
		else throw new Exception("Falha na inserção do recado!");
	}

	public function remove($post_id) {
		$query = $this->query(
			"DELETE c, p FROM CalendarPost cp RIGHT JOIN Calendar AS c ON cp.calendar_id = c.id RIGHT JOIN Post AS p ON cp.post_id = p.id WHERE p.id = :post_id",
			array(":post_id" => $post_id)
		);
		if(!$query["fail"])
			echo $this->control->view->helper->alertSuccess("Recado removido com sucesso!", URL."REDIRECT/POST/SHOW");
		else throw new Exception("Falha na remoção do recado!");
	}

	public function retrievePosts( ) {
		$query = $this->query("SELECT * FROM Post ORDER BY created_on DESC");
		if(!$query["fail"])
			return $query["result"];
		else return NULL;
	}

}

?>
